cc.Class({
    extends: cc.Component,

    properties: {
        txtLevel:cc.Label,//关卡等级
        basketball:cc.Node,//篮球
        btnSetting:cc.Button,//设置按钮
        nodeTanGang:cc.Node,//弹杆
        colliderMgr:null,//碰撞类
        isCollider:false,//是否点击一起碰撞
        qiuKuang_left: cc.Node,//左边球筐
        qiuKuang_rigth:cc.Node,//右边球筐
        levelData:null,//当前关卡数据
        zhongQiuNumber:0,//中球个数
        passQiuLog:cc.Node,//中球tog显示
        nodeQiuPass:cc.Node,//tog节点
        nodeTime:cc.Node,//倒计时节点
        timeProgress:cc.ProgressBar,//倒计时进度条
        starAnim:cc.Prefab,//开始动画
        animWin:cc.Node,//游戏胜利
        KuangDirection:null,//当前球筐位置
        kuangAct:null,//当前球筐位置动画
        nodeTitile:cc.Node,//指导节点
        txtContent:cc.Label,//提示
        nodeGameStart:cc.Node,//提示
        spGood:cc.Node,
        spPerfect:cc.Node,
    },

    onLoad () {
        this.setBasketballSkin();
        this.nodeQiuPass.active = false;
        cc.director.preloadScene("gameEnd");
        this.colliderMgr = this.basketball.getComponent("ColliderMgr");
        let data = window.getLevelData();
        cc.director.getPhysicsManager().enabled = true;
        if(data.levelNumber == 1 || data.gameTime != 0)
        {
            if(data.levelNumber == 1)
            {
                this.setGameLevelContent(data);
            }
            if(window.gameConfig.level <= 1 || data.gameTime != 0)this.showGuideNode(data.levelNumber, data.gameTime, data.allNumber, data);
        }
        else
        {
            // this.basketball.active = true;
            cc.director.getPhysicsManager().enabled = true;
            this.setGameLevelContent(data);
        }

        // this.setGameLevelContent(data);
        window.gameType = "play";
    },

    //设置篮球皮肤
    setBasketballSkin:function(){
        let skin = window.gameConfig.ballSkin;
        let url = "shop/ball/" + skin;
        var ballSkin = this.basketball.getComponent(cc.Sprite);
        window.onLoadSpriteRes(url, cc.SpriteFrame, (frame)=>{
            ballSkin.spriteFrame = frame;
        });

        let GanUrl = "shop/gan/" + window.gameConfig.tangGanSkin;
        var ganSkin = this.nodeTanGang.getComponent(cc.Sprite);
        window.onLoadSpriteRes(GanUrl, cc.SpriteFrame, (frame)=>{
            ganSkin.spriteFrame = frame;
        });
    },

    //显示新手指导界面
    showGuideNode(level, time, number, data){
        let gravity = this.basketball.getComponent(cc.RigidBody);
        gravity.gravityScale = 0;
        if(level == 1)
        {
            this.nodeTitile.active = true;
            this.nodeGameStart.active = true;
            this.txtContent.string = "玩法：点击屏幕任意地方可控制弹杠\n目标：用弹杠将精灵球弹入篮筐\n捕获精灵即可胜利";
        }
        else
        {
            this.nodeTitile.active = true;
            this.nodeGameStart.active = false;
            this.txtContent.string = "请在"+time+"秒内投进"+number+"个球";
        }
        
        var self = this;
        if(level != 1)
        {
            //3秒后开启刚体组件
            this.scheduleOnce(()=>{
                gravity.gravityScale = 4;
                self.nodeTitile.active = false;
                self.setGameLevelContent(data);
                cc.director.getPhysicsManager().enabled = true;
            },2.5)
        }
    },

    //第一关玩家点击屏幕关闭提示界面 开启刚体组件
    onButtonGameStart:function(){
        let gravity = this.basketball.getComponent(cc.RigidBody);
        gravity.gravityScale = 4;
        cc.director.getPhysicsManager().enabled = true;
        this.nodeTitile.active = false;
    },

    start () {
        this.node.on(cc.Node.EventType.TOUCH_START,this.startTouch,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.endTouch,this);
    },

    //开始触摸
    startTouch:function(){
        let self = this;
        this.setTanGangROtation("start");
        if(!this.isCollider)
        {
            this.isCollider = true;
            this.scheduleOnce(() =>{
                self.isCollider = false;
            },0.2);
        }
    },

    //结束触摸
    endTouch:function(){
        this.setTanGangROtation("end");
    },

    //设置弹杆角度
    setTanGangROtation:function(type){
        let act = null;
        if(type == "start")
        {
            act = cc.rotateTo(0.2,-80);
        }
        else if(type == "end")
        {
            act = cc.rotateTo(0.2,3);
        }
        this.nodeTanGang.runAction(act);
    },


    //游戏失败
    onGameOver:function(){
        this.stopQiuKuangAction();
        if(this.zhongQiuNumber >= this.levelData.allNumber)
        {
            window.gameEndData = {
                endType:1,
                gameLevel:this.levelData.levelNumber
            };
        }
        else
        {
            window.gameEndData = {
                endType:0,
                gameLevel:this.levelData.levelNumber
            };
        }
        cc.director.loadScene("gameEnd");
    },

    //移动篮筐
    onMoveQiuKuang:function(nodeQiuKuang,maxY,minY, posX, time){
        var seq = cc.repeatForever(
            cc.sequence(
                cc.moveTo(time, cc.p(posX,maxY)),
                cc.moveTo(time, cc.p(posX,minY))
            ));
        this.kuangAct = {
            "seq":seq,
            "nodeAc":nodeQiuKuang
        };
        nodeQiuKuang.runAction(seq);
    },

    //停止球筐移动动画
    stopQiuKuangAction:function(){
        if(this.kuangAct != null)
        {
            this.kuangAct.nodeAc.stopAction(this.kuangAct.seq);
        }
    },

    //根据当前游戏关卡数据 来设置游戏
    setGameLevelContent:function(data){
        // let data = window.getLevelData();
        if(this.levelData == null)this.levelData = data;
        this.zhongQiuNumber = 0;
        if(this.passQiuLog.children.length > 0)this.passQiuLog.removeAllChildren();
        if(data != null)this.txtLevel.string = "第" + data.levelNumber + "关"
        
        if(this.levelData.gameTime == 0)
        {
            this.nodeTime.active = false;
        }
        else
        {
            this.nodeTime.active = true;
            let smallPro = (0.3 / this.levelData.gameTime).toFixed(4);
            this.setProgressTime(this.levelData.gameTime, smallPro);
        }

        let qiuConten = this.levelData.levelData[0];
        this.setQiuKuangPosition(qiuConten);
        this.setQiuPassNumber(data.allNumber);
    },

    //设置进度条
    setProgressTime(time, smallPro){
        this.scheduleOnce(()=>{
            if(time > 0  && this.timeProgress.progress > 0)
            {
                let pro = this.timeProgress.progress - smallPro;
                if(isNaN(pro))debugger
                this.timeProgress.progress = pro;
                this.setProgressTime(time-0.3, smallPro);
            }else{
                if(this.zhongQiuNumber >= this.levelData.allNumber)
                {
                    window.setPassLevel(this.levelData.levelNumber, true);
                }
                else
                {
                    window.setPassLevel(this.levelData.levelNumber, false);
                }
                this.onGameOver();
            }
        },0.3);
    },

    //设置已中球number
    setQiuPassNumber:function(number){
        for(let i = 0; i < number; i++)
        {
            let passLog = cc.instantiate(this.nodeQiuPass);
            this.passQiuLog.addChild(passLog);
            passLog.active = true;
        }
    },

    //设置球筐位置
    setQiuKuangPosition:function(data){
        if(data == null)return;

        var qiuKuangNode = null;
        var posX = 0;
        this.KuangDirection = data.direction;
        if(data.direction == "left")
        {
            qiuKuangNode = this.qiuKuang_left;
            posX = -343;
            this.qiuKuang_left.active = true;
            this.qiuKuang_rigth.active = false;
        }
        else
        {
            qiuKuangNode = this.qiuKuang_rigth;
            posX = 329;
            this.qiuKuang_left.active = false;
            this.qiuKuang_rigth.active = true;
        }
        
        //球筐是否是自动移动
        if(data.isAuto)
        {
            //计算出球筐移动的最高点和最低点
            let qiuKuangPosiMin = data.kuangPos - data.movePos;
            this.onMoveQiuKuang(qiuKuangNode, data.kuangPos, qiuKuangPosiMin, posX, data.moveTime);
        }
        else
        {
            //设置球筐位置
            qiuKuangNode.setPosition(posX, data.kuangPos);
        }
    },

    //中球
    inTheBall:function(){
        if(this.zhongQiuNumber >= this.levelData.allNumber)return
        this.showPinYu();
        let qiuCheck = this.passQiuLog.children[this.zhongQiuNumber].getChildByName("check");
        qiuCheck.active = true;
        qiuCheck.getComponent(cc.Animation).play();

        var nodeAnim = window.creatrPrefab(this.starAnim);

        let nodeKuang = null;
        if(this.levelData.levelData[this.zhongQiuNumber].direction == "left")
        {
            nodeAnim.setPosition(110,0);
            nodeKuang = this.qiuKuang_left
        }
        else
        {
            nodeAnim.setPosition(-100,0);
            nodeKuang = this.qiuKuang_rigth;
        }

        let anima = nodeAnim.getComponent(cc.Animation);
        nodeKuang.addChild(nodeAnim);
        let animTime = anima.play();

        this.scheduleOnce(() =>{
            this.zhongQiuNumber = this.zhongQiuNumber + 1;
            let des = nodeKuang.getChildByName("starAnim");
            des.removeFromParent();
            if(this.zhongQiuNumber >= this.levelData.allNumber)
            {
                let qiuAnim = this.basketball.getComponent(cc.Animation);
                let qiuTime = qiuAnim.play();
                window.gameType = "win";
                this.scheduleOnce(()=>{
                    this.animWin.active = true;
                    window.audioMgr.prototype.playGameWinAudio();
                    this.scheduleOnce(()=>{
                        this.animWin.active = false;
                        window.setPassLevel(this.levelData.levelNumber, true);
                        this.onGameOver();
                    },3);
                },qiuTime.duration);
            }
            else
            {
                this.stopQiuKuangAction();
                this.setQiuKuangPosition(this.levelData.levelData[this.zhongQiuNumber]);
            }
        },animTime.duration)
    },

    //显示评语
    showPinYu:function(){
        let spNode;
        let ran = Math.random();
        if(ran >= 0.5)
        {
            spNode = this.spGood;
        }
        else
        {
            spNode = this.spPerfect;
        }

        spNode.active = true;

        this.scheduleOnce(()=>{
            spNode.active = false;
        },1)
    },
    //播放中球时球网的动画
    showPassQiuAnim:function(){
        let wangAnim = null;
        if(this.KuangDirection == "left")
        {
            wangAnim = this.qiuKuang_left.getChildByName("wang").getComponent(cc.Animation);
        }
        else
        {
            wangAnim = this.qiuKuang_rigth.getChildByName("wang").getComponent(cc.Animation);
        }

        if(window.gameType != "win" && window.gameType != "lose")window.audioMgr.prototype.playQiuPassAudio();
        wangAnim.play();
    },
});
